package com.pengpai.pangucms.utils;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

@Data
@Accessors(chain = true)
public class RWang implements Serializable {

    @Serial
    private static final long serialVersionUID = -11L;

    private Integer errno;

    private String message;

    private Object data;

    public RWang(Integer errno, String message, Object data) {
        super();
        this.errno = errno;
        this.message = message;
        this.data = data;
    }

    private static RWang build(Integer code, String msg, Object data) {
        return new RWang(code, msg, data);
    }

    public static RWang success() {
        return new RWang(0, HTTPStatusCode.SUCCESS.msg, null);
    }

    public static RWang success(Object data) {
        return build(HTTPStatusCode.SUCCESS.code, HTTPStatusCode.SUCCESS.msg, data);
    }

    public static RWang fail() {
        return fail(HTTPStatusCode.ERROR.msg);
    }

    public static RWang fail(String msg) {
        return fail(1, msg);
    }

    public static RWang fail(HTTPStatusCode httpStatusCode) {
        return fail(1, httpStatusCode.msg);
    }

    public static RWang fail(Integer errno, String msg) {
        return build(errno, msg, null);
    }

    public enum HTTPStatusCode {
        /**
         * 业务码
         */
        SUCCESS(200, "操作成功！"),
        FORBIDDEN(403,"没有权限!"),
        METHOD_NOT_ALLOWED(405,"请求方式错误!"),
        ERROR(500, "系统异常！");



        HTTPStatusCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        private final int code;
        private final String msg;

        public int getCode() {
            return code;
        }

        public String getMessage() {
            return msg;
        }
    }
}